/*
	Copyright (c) 2008, Grant Jones <grant@podtronics.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

		Redistributions of source code must retain the above copyright
		notice, this list of conditions and the following disclaimer.
		
		Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in
		the documentation and/or other materials provided with the
		distribution.
		
		Neither the name of the gxjones.com nor the names of its
		contributors may be used to endorse or promote products derived
		from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
	OF THE POSSIBILITY OF SUCH DAMAGE.
	
	http://www.opensource.org/licenses/bsd-license.php
*/

/*
 *  radixtree.h
 *  radixtree
 *
 *  Created by Grant Jones on 2/29/08.
 *
 */
 
/*
** Description:
** The radixtree module is a byte-resolution implementation of a
** radix tree. This tree is implemented upon an underlying "tag"
** system which represent key/value pairs (tagm/tagmt modules).
**
** The tag's key describes a subsection of the original key string
** split up based on the rules of a radix tree. Note: this is not
** a true representation of a radix tree - its resolution is at
** a byte level instead of bit level. This means binary data may
** result in tree's with 256 branches off one node, which will not
** perform well (with the currently used linear search).
** 
**
** Areas of improvement:
** - bit-resolution, true radix tree implementation
** - the linear key-fragment search really should be improved to
**   do a binary search - which would required an order key-
**   fragment list.
**
*/
#ifndef RADIXTREE_H
#define RADIXTREE_H
#include "tagm.h"
#include "tagmt.h"
#include "radixtree_bitops.h"

#define RTREE_BRANCH_PTRS_SZ	sizeof(off_t)*4
	
typedef struct
{
	TagManagerStruct *tms;
	off_t list_cursor;
} rtree_t;

typedef struct
{
	size_t size;
	void *data;
} rtvalue_t;



int rtree_open( rtree_t *rt, const char *file, int flags );
int rtree_close( rtree_t *rt );


int rtree_use( rtree_t *rt, const char *key, size_t key_sz );

off_t rt_root_get( rtree_t *rt, rtvalue_t *key, rtvalue_t *data );
int rt_root_set( rtree_t *rt, rtvalue_t *key, rtvalue_t *data, off_t *tag_loc );


/* rtree interface */
int rt_get( rtree_t *rt, rtvalue_t *key, rtvalue_t *data );
int rt_put( rtree_t *rt, rtvalue_t *key, rtvalue_t *data );
int rt_del( rtree_t *rt, rtvalue_t *key, int bOverflow );
int rt_app( rtree_t *rt, rtvalue_t *key, rtvalue_t *append_data );

/* internal routines */

int rt_split_tag( rtree_t *rt, off_t tag_loc, size_t crit_key_loc, off_t *new_tag_loc );
int rt_insert_keyed_tag( TagManagerStruct *tags, off_t root_loc, off_t *tag_loc,  unsigned char *key, size_t key_sz, unsigned char *data, size_t data_sz, unsigned char bFlags );
int rt_remove_overflow( rtree_t *rt, off_t parent_tag_loc );
off_t rt_locate_key( rtree_t *rt, rt_bit_string_t *key_bstr, size_t *last_step_loc, rt_bit_string_t **key_bstr_left, int *result );
int rt_put_raw( rtree_t *rt, rtvalue_t *key, rtvalue_t *data, int bRemoveOverflow );

#endif


